import { http } from "@/utils/http";
// import { getToken } from "@/utils/auth";
import { getToken175, getToken43 } from "@/utils/token";

type Result = {
  message: string;
  data: any;
  code: number;
};

// 为task API添加Authorization头的辅助函数
const taskRequest = <T>(method: string, url: string, data?: object) => {
  const token = getToken175(); // 每次调用都获取最新的token
  return http.request<T>(method as any, url, {
    data,
    headers: {
      Authorization: token ? `Bearer ${token}` : undefined,
    },
  });
};

const remoteRequest = <T>(method: string, url: string, data?: object) => {
  const remoteToken = getToken43(); // 每次调用都获取最新的token
  return http.request<T>(method as any, url, {
    data,
    headers: {
      Authorization: remoteToken ? `Bearer ${remoteToken}` : undefined,
    },
  });
};

/** 获取资产列表 */
export const getAssetList = ({
  data,
  type = "asset",
}: {
  data: object;
  type: string;
}) => {
  return taskRequest<Result>("post", `/api/api/${type}/data`, data);
};

/** 获取资产详情 */
export const getAssetDetail = (data?: object) => {
  return taskRequest<Result>("post", "/api/api/asset/detail", data);
};

/** 获取资产变更日志 */
export const getAssetChangelog = (data?: object) => {
  return taskRequest<Result>("post", "/api/api/asset/changelog", data);
};

/** 删除资产 */
export const deleteAsset = (data?: object) => {
  return taskRequest<Result>("post", "/api/api/data/delete", data);
};

/** 添加标签 */
export const addTag = (data?: object) => {
  return taskRequest<Result>("post", "/api/api/data/add/tag", data);
};

/** 删除标签 */
export const deleteTag = (data?: object) => {
  return taskRequest<Result>("post", "/api/api/data/delete/tag", data);
};

/** 获取资产总数 */
export const getAssetTotal = (data?: object) => {
  return taskRequest<Result>("post", "/api/api/data/total", data);
};

/** 获取异地资产列表 */
export const getRemoteAssetList = (data?: object) => {
  return remoteRequest<Result>("post", "/api/api2/asset/data", data);
};

/** 获取异地资产总数 */
export const getRemoteAssetTotal = (data?: object) => {
  return remoteRequest<Result>("post", "/api/api2/data/total", data);
};

/** 获取异地资产详情 */
export const getRemoteAssetDetail = (data?: object) => {
  return remoteRequest<Result>("post", "/api/api2/asset/detail", data);
};

/** 删除异地资产 */
export const deleteRemoteAsset = (data?: object) => {
  return remoteRequest<Result>("post", "/api/api2/data/delete", data);
};

/** 添加异地标签 */
export const addRemoteTag = (data?: object) => {
  return remoteRequest<Result>("post", "/api/api2/data/add/tag", data);
};

/** 删除异地标签 */
export const deleteRemoteTag = (data?: object) => {
  return remoteRequest<Result>("post", "/api/api2/data/delete/tag", data);
};

/** 获取敏感信息结果 */
export const getSensitiveResult = (data?: object) => {
  return taskRequest<Result>("post", "/api/api/sensitive/result/data2", data);
};

/** 获取异地资产变更日志 */
export const getRemoteAssetChangelog = (data?: object) => {
  return remoteRequest<Result>("post", "/api/api2/asset/changelog", data);
};

/** 更新资产状态 */
export const updateAssetStatus = (data?: object) => {
  return taskRequest<Result>("post", "/api/api/data/update/status", data);
};

// ========== 资产管理平台相关接口 ==========

/** 上传资产文件 */
export const uploadAssetFile = (data: FormData) => {
  const token = getToken175();
  return http.request<Result>("post", "/api/assetManager/upload", {
    data,
    headers: {
      Authorization: token ? `Bearer ${token}` : undefined,
      "Content-Type": "multipart/form-data",
    },
  });
};

/** 查询任务列表 */
export const queryTaskList = (data?: {
  page?: number;
  per_page?: number;
  task_name?: string;
}) => {
  const token = getToken175();
  return http.request<Result>("post", "/api/assetManager/query_task", {
    data,
    headers: {
      Authorization: token ? `Bearer ${token}` : undefined,
    },
  });
};

/** 查询任务下的数据 */
export const queryTaskData = (data: {
  task_id: number;
  page?: number;
  per_page?: number;
  asset_name?: string;
  exposure_asset_ip?: string;
  service_type?: string;
  asset_type?: string;
  department?: string;
}) => {
  const token = getToken175();
  return http.request<Result>("post", "/api/assetManager/query_task_data", {
    data,
    headers: {
      Authorization: token ? `Bearer ${token}` : undefined,
    },
  });
};

/** 删除任务 */
export const deleteTask = (data: { task_id: number }) => {
  const token = getToken175();
  return http.request<Result>("post", "/api/assetManager/delete_task", {
    data,
    headers: {
      Authorization: token ? `Bearer ${token}` : undefined,
    },
  });
};
